﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Text;
using System.Web.SessionState;

namespace Web.common
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class XAjax : IHttpHandler, IRequiresSessionState
    {

        private static HttpContext content;
        public void ProcessRequest(HttpContext c)
        {
            content = c;
            string realIP = Web.Utils.GetIP();
            //if (Security.Instance._ForbidIP.Contains(realIP))
            //{
            //    this.ResponseXML(Result.Failed, "-禁止访问-");
            //}
            //通过ActionID  来执行方法
            if (c.Request.QueryString["ac"] != null)
            {
                switch (c.Request.QueryString["ac"].Trim().ToLower())
                {
                    case "xy001":
                    case "xy002":
                    case "xy003":
                    case "xy004":
                    case "xy005":
                    case "xy006":
                    case "xy007":
                    case "xy008":
                    case "xy009":
                    case "xy010":
                        //共同代码
                        return;
                    case "xy011":
                        //Ajax异步操作
                        this.ResponseXML(Result.Success);
                        return;
                    default:
                        return;
                }
            }


        }

        private void ResponseXML(Result result)
        {
            this.ResponseXML(result, "", "");
        }

        private void ResponseXML(Result result, string msg)
        {
            this.ResponseXML(result, msg, "");
        }

        private void ResponseXML(Result result, string msg, string data)
        {
            //fordebug


            StringBuilder builder = new StringBuilder();
            builder.Append("var " + GetQueryString("ajaxidnex") + " = '<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            builder.Append("<XYMsg>");
            builder.Append("<state>");
            builder.Append("<result>" + ((int)result) + "</result>");
            builder.Append("<message>" + msg + "</message>");
            builder.Append("</state>");
            builder.Append("<data>");
            builder.Append(System.Text.RegularExpressions.Regex.Replace(data.Replace("'", "\""), "[\n-\r]", ""));
            builder.Append("</data>");
            builder.Append("</XYMsg>'");
            content.Response.Write(builder.ToString());
            content.Response.End();
        }

        private void ResponseXMLNojs(Result result, string msg)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/XML";
            System.Web.HttpContext.Current.Response.Charset = "UTF-8";
            System.Web.HttpContext.Current.Response.Expires = 0;
            System.Web.HttpContext.Current.Response.Cache.SetNoStore();
            string builder = string.Empty;
            builder = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<derusunmsg><result>" + ((int)result) + "</result><message>" + msg + "</message></derusunmsg>";
            System.Web.HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
            System.Web.HttpContext.Current.Response.Write(builder);
            System.Web.HttpContext.Current.Response.End();
        }
        private enum Result
        {
            Failed = 0,
            Null = -1,
            Success = 1
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        //Request处理
        public static string GetQueryString(string strName)
        {
            if (HttpContext.Current.Request.QueryString[strName] == null)
            {
                return "";
            }
            return HttpContext.Current.Request.QueryString[strName].ToString().Trim();
        }

    }
}
